package com.it.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.it.mapper.CompanyMapper;
import com.it.mapper.OutDisposalMapper;
import com.it.mapper.UserInfoMapper;
import com.it.pojo.*;
import com.it.pojo.dto.OutDisposalDto;
import com.it.service.OutdisposalService;
import com.it.util.CodeUtils;
import com.it.util.TimeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class OutdisposalServiceImpl implements OutdisposalService {
    @Autowired
    private OutDisposalMapper outdisposalMapper;
    @Autowired
    private UserInfoMapper userinfoMapper;
    @Autowired
    private CompanyMapper companyMapper;


    @Override
    public int addOutdisposal(OutStore outstore,User user) {
        OutDisposal outdisposal=new OutDisposal();
        BeanUtils.copyProperties(outstore,outdisposal);
        outdisposal.setDisposalId(getPrimaryKey());
        outdisposal.setDisposalCode(CodeUtils.getCodeByCountDP(Integer.parseInt(getPrimaryKey())));
        outdisposal.setDisposalTime(new Date());
        outdisposal.setDisposalNum(outstore.getOutNum().intValue());
        outdisposal.setDisposalUnit(outstore.getOutUnit());
//        outdisposal.setDisposalType("1");
        if(outstore.getOutType().equals("1")){
            outdisposal.setDisposalType("焚烧");
        } else if (outstore.getOutType().equals("2")) {
            outdisposal.setDisposalType("水载");
        }else if (outstore.getOutType().equals("3")){
            outdisposal.setDisposalType("填埋");
        }else{
            outdisposal.setDisposalType(null);
        }

        outdisposal.setDisposalDel(0);

        UserInfo userinfo = userinfoMapper.selectByPrimaryKey(user.getUserId());//通过uid查uinfo
        Company company = companyMapper.selectByPrimaryKey(userinfo.getCompanyId());//通过公司id查公司信息
        outdisposal.setCompanyName(company.getCompanyName());
        outdisposal.setCompanyLegal(company.getCompanyLegal());
        outdisposal.setCompanyCertificate(company.getCompanyCertificate());


        int i = outdisposalMapper.insertSelective(outdisposal);
        return i;
    }

    @Override
    public PageInfo<OutDisposal> findPage(OutDisposalDto dto) {
        if (dto.getPageNo() == null) {
            dto.setPageNo(1);
        }
        PageHelper.startPage(dto.getPageNo(), dto.getPageSize());
        OutDisposalExample example = new OutDisposalExample();
        OutDisposalExample.Criteria criteria = example.createCriteria();
        if (dto.getUserId() != null && dto.getUserId() != -1) {
            criteria.andUserIdEqualTo(String.valueOf(dto.getUserId()));
        }
        if (dto.getTime() != null && dto.getTime() != -1) {
            switch (dto.getTime()) {
                case 1:
                    criteria.andDisposalTimeBetween(TimeUtils.startDay(), TimeUtils.endDay());
                    break;
                case 2:
                    criteria.andDisposalTimeBetween(TimeUtils.startMonth(), TimeUtils.endMonth());
                    break;
                case 3:
                    criteria.andDisposalTimeBetween(TimeUtils.startSeason(), TimeUtils.endSeason());
                    break;
                case 4:
                    criteria.andDisposalTimeBetween(TimeUtils.startYear(), TimeUtils.endYear());
                    break;
            }
        }
        example.setOrderByClause("disposal_time asc");
        List<OutDisposal> list = outdisposalMapper.selectByExample(example);
        PageInfo<OutDisposal> pageInfo = new PageInfo<>(list, 5);
        return pageInfo;
    }

    @Override
    public List<OutDisposal> findExcel(OutDisposalDto dto) {
        OutDisposalExample example = new OutDisposalExample();
        OutDisposalExample.Criteria criteria = example.createCriteria();
        if (dto.getUserId() != null && dto.getUserId() != -1) {
            criteria.andUserIdEqualTo(String.valueOf(dto.getUserId()));
        }
        if (dto.getTime() != null && dto.getTime() != -1) {
            switch (dto.getTime()) {
                case 1:
                    criteria.andDisposalTimeBetween(TimeUtils.startDay(), TimeUtils.endDay());
                    break;
                case 2:
                    criteria.andDisposalTimeBetween(TimeUtils.startMonth(), TimeUtils.endMonth());
                    break;
                case 3:
                    criteria.andDisposalTimeBetween(TimeUtils.startSeason(), TimeUtils.endSeason());
                    break;
                case 4:
                    criteria.andDisposalTimeBetween(TimeUtils.startYear(), TimeUtils.endYear());
                    break;
            }
        }
        example.setOrderByClause("disposal_time asc");
        return outdisposalMapper.selectByExample(example);
    }

    public String getPrimaryKey(){
        OutDisposalExample outdisposalExample=new OutDisposalExample();
        List<OutDisposal> outdisposals = outdisposalMapper.selectByExample(outdisposalExample);
        return outdisposals.size()+1+"";
    }
}
